pub fn u16_to_byte(num: u16) -> [u8; 2] {
    let mut bytes: [u8; 2] = [0; 2];

    bytes[0] = ((num & 0xFF00) >> 8) as u8;
    bytes[1] = (num & 0xFF) as u8;

    bytes
}

pub fn vec_in_u16(v: &mut Vec<u8>, num: u16) {
    v.push(((num & 0xFF00) >> 8) as u8);
    v.push((num & 0xFF) as u8);
}

pub fn byte_to_u16(bytes: &[u8]) -> u16 {
    if bytes.len() < 2 {
        return 0;
    }
    ((bytes[0] as u16) << 8) | (bytes[1] as u16)
}

//------------------------------------------------------------//

pub fn u32_to_byte(num: u32) -> [u8; 4] {
    let mut bytes: [u8; 4] = [0; 4];

    bytes[0] = ((num & 0xFF000000) >> 24) as u8;
    bytes[1] = ((num & 0xFF0000) >> 16) as u8;
    bytes[2] = ((num & 0xFF00) >> 8) as u8;
    bytes[3] = (num & 0xFF) as u8;

    bytes
}

pub fn vec_in_u32(v: &mut Vec<u8>, num: u32) {
    v.push(((num & 0xFF000000) >> 24) as u8);
    v.push(((num & 0xFF0000) >> 16) as u8);
    v.push(((num & 0xFF00) >> 8) as u8);
    v.push((num & 0xFF) as u8);
}

pub fn byte_to_u32(bytes: &[u8]) -> u32 {
    if bytes.len() < 4 {
        return 0;
    }
    ((bytes[0] as u32) << 24)
        | ((bytes[1] as u32) << 16)
        | ((bytes[2] as u32) << 8)
        | (bytes[3] as u32)
}

//------------------------------------------------------------//

pub fn u64_to_byte(num: u64) -> [u8; 8] {
    let mut bytes: [u8; 8] = [0; 8];

    bytes[0] = ((num & 0xFF00000000000000) >> 56) as u8;
    bytes[1] = ((num & 0xFF000000000000) >> 48) as u8;
    bytes[2] = ((num & 0xFF0000000000) >> 40) as u8;
    bytes[3] = ((num & 0xFF00000000) >> 32) as u8;

    bytes[4] = ((num & 0xFF000000) >> 24) as u8;
    bytes[5] = ((num & 0xFF0000) >> 16) as u8;
    bytes[6] = ((num & 0xFF00) >> 8) as u8;
    bytes[7] = (num & 0xFF) as u8;

    bytes
}

pub fn vec_in_u64(v: &mut Vec<u8>, num: u64) {
    v.push(((num & 0xFF00000000000000) >> 56) as u8);
    v.push(((num & 0xFF000000000000) >> 48) as u8);
    v.push(((num & 0xFF0000000000) >> 40) as u8);
    v.push(((num & 0xFF00000000) >> 32) as u8);

    v.push(((num & 0xFF000000) >> 24) as u8);
    v.push(((num & 0xFF0000) >> 16) as u8);
    v.push(((num & 0xFF00) >> 8) as u8);
    v.push((num & 0xFF) as u8);
}

pub fn byte_to_u64(bytes: &[u8]) -> u64 {
    if bytes.len() < 8 {
        return 0;
    }
    ((bytes[1] as u64) << 56)
        | ((bytes[2] as u64) << 48)
        | ((bytes[3] as u64) << 40)
        | ((bytes[4] as u64) << 32)
        
        | ((bytes[5] as u64) << 24)
        | ((bytes[6] as u64) << 16)
        | ((bytes[7] as u64) << 8)
        | (bytes[8] as u64)
}
